package cn.walket.test.db;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import java.util.ArrayList;
import java.util.List;

public class CodeGenerator {

  private static final String TABLE_PREFIX = "t_";
  private static final String LOGIC_DELETE_FIELD_NAME = "is_deleted";

  public static void main(String[] args) {
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    System.out.println(System.getProperty("user.project"));
    String projectPath = System.getProperty("user.dir") + "/user-server";
    gc.setOutputDir(projectPath + "/src/main/java");
    gc.setOpen(false);
    gc.setFileOverride(true);
//    gc.setEntitySuffix("Po");
    mpg.setGlobalConfig(gc);
    // 数据源配置
    IbfsDataSourceConfig datasourceConfig = getDatasourceConfig();
    mpg.setDataSource(datasourceConfig);

    // 包配置
    PackageConfig pc = new PackageConfig();
    pc.setParent("cn.walket.cloud.user.dao");
    mpg.setPackageInfo(pc);

    // 自定义配置
    InjectionConfig cfg = new InjectionConfig() {
      @Override
      public void initMap() {

      }
    };
    // 如果模板引擎是 velocity
    String templatePath = "/templates/mapper.xml.vm";
    List<FileOutConfig> focList = new ArrayList<>();
    focList.add(new FileOutConfig(templatePath) {
      @Override
      public String outputFile(TableInfo tableInfo) {
        String mapperName = tableInfo.getMapperName();
        return projectPath + "/src/main/resources/mapper/" + pc.getModuleName() + "/" + mapperName + StringPool.DOT_XML;
//        return projectPath + "/src/main/resources/mapper/" + pc.getModuleName() + "/" + mapperName + StringPool.DOT_XML;
      }
    });
    cfg.setFileOutConfigList(focList);
    mpg.setCfg(cfg);
    // 配置模板
    TemplateConfig templateConfig = new TemplateConfig();
    // 配置自定义输出模板
    //指定自定义模板路径，注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
    templateConfig.setEntity("templates/entity2.java");
    templateConfig.setXml(null);
    templateConfig.setController(null);
    templateConfig.setService(null);
    templateConfig.setServiceImpl(null);
    mpg.setTemplate(templateConfig);
    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    strategy.setSkipView(true);
    strategy.setNaming(NamingStrategy.underline_to_camel);
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    strategy.setLogicDeleteFieldName(LOGIC_DELETE_FIELD_NAME);
    strategy.setEntityLombokModel(true);
//    strategy.setSuperEntityClass(BasePo.class);
//    strategy.setSuperEntityColumns("id","is_deleted","create_time","update_time");
    strategy.setTableIncludeCallback(() -> datasourceConfig.getTables().toArray(new String[]{}));
//    strategy.setInclude();
//    strategy.setExclude()
    strategy.setControllerMappingHyphenStyle(true);
    strategy.setTablePrefix(TABLE_PREFIX);
    strategy.setTablePrefix("t_");
    strategy.setCapitalMode(false);
    mpg.setStrategy(strategy);
    mpg.setTemplateEngine(new VelocityTemplateEngine());
    mpg.execute();
  }

  private static IbfsDataSourceConfig getDatasourceConfig() {
    IbfsDataSourceConfig dsc = new IbfsDataSourceConfig();
              //jdbc:h2:~/test;MODE=MySQL;DATABASE_TO_LOWER=TRUE
    dsc.setUrl("jdbc:h2:mem:test;mode=MySQL;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;");
    dsc.setDriverName("org.h2.Driver");
    dsc.setUsername("sa");
    dsc.setPassword("");
    return dsc;
  }

}